package com.xyz.tag.html.attribute;

import java.util.HashSet;
import java.util.Set;

import com.xyz.tag.html.XYHtmlConstants;
import com.xyz.tag.util.StringUtil;


public class XYAttributeStyle extends XYAttribute {

  private Set<XYAttributeStyleValue> styles = null;

  public XYAttributeStyle() {
    super( XYHtmlConstants.attribute_style );
  }

  public XYAttributeStyle addStyle( String name, String value ) {
    if ( !StringUtil.isEmpty( name ) && !StringUtil.isEmpty( value ) ) {
      addStyle( new XYAttributeStyleValue( name, value ) );
    }
    return this;
  }

  public XYAttributeStyle addStyle( XYAttributeStyleValue styleValue ) {
    if ( styles == null ) {
      styles = new HashSet<XYAttributeStyleValue>();
    }
    styles.add( styleValue );
    return this;
  }

  @Override
  public String draw() {
    StringBuffer html = new StringBuffer();

    if ( styles != null && !styles.isEmpty() ) {
      html.append( XYHtmlConstants.attribute_style ).append( XYHtmlConstants.sign_eq )
          .append( XYHtmlConstants.sign_quot );
      for ( XYAttributeStyleValue styleValue : styles ) {
        html.append( styleValue.getName() ).append( XYHtmlConstants.sign_colon ).append( styleValue.getValue() )
            .append( XYHtmlConstants.sign_semicolon );
      }
      html.append( XYHtmlConstants.sign_quot );
    }

    return html.toString();
  }

  private class XYAttributeStyleValue {

    private String name;
    private String value;

    public XYAttributeStyleValue( String name, String value ) {
      this.name = name;
      this.value = value;
    }

    @Override
    public int hashCode() {
      return name.hashCode();
    }

    @Override
    public boolean equals( Object o ) {
      return name.equalsIgnoreCase( ( (XYAttributeStyleValue)o ).getName() );
    }

    public String getName() {
      return name;
    }

    public String getValue() {
      return value;
    }
  }

  private static final long serialVersionUID = 9006660492122768338L;

}
